function trip(obj, trip) {
    document.getElementById(obj).innerHTML = "<b>" + trip + "</b>";
}

function checkForm() {
    //获取用户名输入项
    var userNname = document.getElementById("userName");
    var uName = userNname.value;
    if (uName.length < 1 || uName.length > 10) {
        trip("name_trip", "错误:账号长度在1-10之间");
        return false;
    } else {
        trip("name_trip", "正确!");
    }

    // 密码长度大于6 和确认必须一致 
    var password = document.getElementById("password");
    var userPass = password.value;
    if (userPass.length < 6) {
        trip("password_trip", "错误:秘密长度在6-12之间");
        return false;
    } else {
        trip("password_trip", "正确!");
    }

    //获取确认密码框的值 var
    var surePassword = document.getElementById("surePassword").value;
    if (userPass !== surePassword) {
        trip("surePassword_trip", "错误:两次密码不一致");
        return false;
    } else {
        trip("surePassword_trip", "正确!");
    }

    //校验邮箱(正则表达式):/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
    var inputEmail = document.getElementById("email");
    var uEmail = inputEmail.value;
    if (!/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/.test(uEmail)) {
        trip("emil_trip", "错误:输入的邮箱格式不对");
        return false;
    } else {
        trip("emil_trip", "正确!");
    }
    return true;
}
// 加密
function securityEncode(pwd) {
    let strDe = "RDpbLfCPsJZ7fiv";
    let dictionary = "yLwVl0zKqws7LgKPRQ84Mdt708T1qQ3Ha7xv3H7NyU84p21BriUWBU43odz3iP4rBL3cD02KZciX" +
        "TysVXiV8ngg6vL48rPJyAUw0HurW20xqxv9aYb4M9wK1Ae0wlro510qXeU07kV57fQMc8L6aLgML" +
        "wygtc0F10a0Dg70TOoouyFhdysuRMO51yY5ZlOZZLEal1h0t9YQW0Ko7oBwmCAHoic4HYbUyVeU3" +
        "sfQ1xtXcPcf1aT303wAQhv66qzW";
    let output = "";
    let lenMax, len1, len2, lenDict;
    let cl = 0xBB, cr = 0xBB;
    len1 = strDe.length;
    len2 = pwd.length;
    lenDict = dictionary.length;
    lenMax = len1 > len2 ? len1 : len2;
    for (let index = 0; index < lenMax; index++) {
        cl = cr = 0xBB;
        if (index < len1)
            cl = strDe.charCodeAt(index);
        if (index < len2)
            cr = pwd.charCodeAt(index);
        output += dictionary.charAt((cl ^ cr) % lenDict);
    }
    return output
}
//提交
function submitT() {
    if (checkForm()) {
        //将from表单中的数据 序列化
        let fromData
        let userName = $("#userName").val();
        let userPassword = $("#password").val();
        let userEmail = $("#email").val();
        fromData = {
            "userName": userName,
            "userPassword": securityEncode(userPassword),
            "userEmail": userEmail
        }
        $.ajax({
            url:"registerServlet",
            data:fromData,
            type:"post",
            dataType:"json",
            success :function (res){
                console.log(res)
                if(res.code===200){
                    alert(res.msg)
                    myFunctionReset();
                    window.location.href = "./login.html";
                }else {
                    alert(res.msg)

                }
            },
            error:function (e1,e2,e3){
                console.log(e1+e2+e3)
            }
        })

    } else {
    }
}

//重置事件
function myFunctionReset() {
    trip("name_trip", "&nbsp;");
    trip("password_trip", "&nbsp;");
    trip("surePassword_trip", "&nbsp;")
    trip("emil_trip", "&nbsp;")
    $(':input', '#register_from')  //'#addMenuModal'为form表单id值
        .not(':button', ':submit', ':reset', ':hidden') //排除form中type为button,submit,reset,hidden的input元素
        .val('')  //将input的value设置为空
        .removeAttr('checked'); // 删除radio/checkbox/select中的checked或selected属性
}